package se.liu.oschi129.game.objects;

import java.awt.Image;

import se.liu.oschi129.animation.Animation;
import se.liu.oschi129.world.model.World;
import se.liu.oschi129.world.model.WorldGraphics;
import se.liu.oschi129.world.objects.WorldStaticObject;

/**
 * This object draws a background bottommost in the world. The background can
 * be horizontally or/and vertically repeated.
 * 
 * @author oschi129
 */
public class ObjectBackground extends WorldStaticObject {
	
	// The background animation
	private Animation background;
	
	// Horizontal/Vertical repetition of the background
	private boolean horizontalRep, verticalRep;
	
	/**
	 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	 * - - - - - - - - - - - - Constructors - - - - - - - - - - - 
	 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	 */
	
	public ObjectBackground(World world, boolean horizontalRep, boolean verticalRep, Animation background, double x, double y) {
		super(x,y,world.getWidth(),world.getHeight());
		this.background = background;
		this.horizontalRep = horizontalRep;
		this.verticalRep = verticalRep;
		setDepth(100);
		setVisible(true);
	}
	
	/**
	 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	 * - - - - - - - - - - - Public methods - - - - - - - - - - 
	 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	 */
	
	@Override public void performDrawingAction(WorldGraphics worldg) {
		Image img = background.subimage((int) getImageIndex());
		int dx = img.getWidth(null);
		int dy = img.getHeight(null);
		double x = getX(), y;
		do {
			y = getY();
			do {
				worldg.drawAnimation(x, y, background, (int) getImageIndex());
				y += dy;
			} while (y < getHeight() && verticalRep);
			x += dx;
		} while (x < getWidth() && horizontalRep);
	}
	
}
